﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.Logging;
using System.Diagnostics;
using SpringNetCheckerCore;
using System.IO;

namespace SpringNetObjectsChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            //檢查輸入值
            if (args.Length < 2)
            {
                Console.WriteLine(@"請輸入 參數1:[Spring Objects Xml 設定檔位置] 參數2:[其他設定檔位置] 參數3:[其他設定檔位置] ... 參數最後一個:[使用到的組件檔資料夾位置]");
                Console.WriteLine(@"ex. SpringNetObjectsChecker_net4 C:\web\objects.xml dao.xml C:\web\bin");
                Console.WriteLine(@"Press Any Key ..." + "\n");
                Console.ReadKey();
                return;
            }

            //初始化記錄檔
            FileLogTraceListener tempListener = new FileLogTraceListener();
            tempListener.BaseFileName = string.Format("Log-{0:yyyy-MM-dd-HHmmss}", DateTime.Now);
            tempListener.Location = LogFileLocation.Custom;
            tempListener.CustomLocation = Environment.CurrentDirectory + @"\Log";
            //tempListener.LogFileCreationSchedule = LogFileCreationScheduleOption.Daily;
            tempListener.AutoFlush = true;
            tempListener.Append = true;
            Trace.Listeners.Add(tempListener);

            //新增檢查者
            ISpringNetObjectsChecker springNetObjectsChecker =
                new SpringNetCheckerCore.SpringNetObjectsChecker();

            //新增 Spring Objects XML Schema 資訊
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net",
                Properties.Settings.Default.SpringObjectsSchemaFilePath);
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net/database",
                Properties.Settings.Default.SpringDatabaseSchemaFilePath);
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net/aop",
                Properties.Settings.Default.SpringAopSchemaFilePath);
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net/ems",
                Properties.Settings.Default.SpringEmsSchemaFilePath);
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net/nms",
                Properties.Settings.Default.SpringNmsSchemaFilePath);
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net/nvelocity",
                Properties.Settings.Default.SpringNvelocitySchemaFilePath);
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net/remoting",
                Properties.Settings.Default.SpringRemotingSchemaFilePath);
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net/tool",
                Properties.Settings.Default.SpringToolSchemaFilePath);
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net/tx",
                Properties.Settings.Default.SpringTxSchemaFilePath);
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net/validation",
                Properties.Settings.Default.SpringValidationSchemaFilePath);
            springNetObjectsChecker.AddXmlSchemaInfo("http://www.springframework.net/wcf",
                Properties.Settings.Default.SpringWcfSchemaFilePath);

            //掛上寫Log的方法
            springNetObjectsChecker.AddLogWriteLineDelegate(new LogWriteLineDelegate(LogWriteLine));

            //新增 Spring Objects Xml 設定檔位置
            string firstDirPath = "";
            for (int i = 0; i < args.Length - 1; i++)
            {
                string objectsXmlFilePath = args[i];

                if (i == 0)
                {
                    //取得第一個檔案的路徑
                    firstDirPath = Path.GetDirectoryName(args[i]);
                }
                else
                {
                    //第二個之後沒路徑的,延用第一個檔案的路徑
                    if (Path.GetDirectoryName(objectsXmlFilePath).Length == 0)
                        objectsXmlFilePath = firstDirPath + "\\" + objectsXmlFilePath;
                }

                springNetObjectsChecker.AddObjectsXmlFilePath(objectsXmlFilePath);
            }

            string assembliesDirectoryPath = args[args.Length - 1];

            //檢查 Spring Objects Xml 設定檔
            bool result = springNetObjectsChecker.CheckObjectsXmlFile(assembliesDirectoryPath);

            tempListener.Flush();
        }

        /// <summary>
        /// 顯示並記錄訊息
        /// </summary>
        static void LogWriteLine(string value)
        {
            //Trace.TraceInformation("{0:yyyy/MM/dd HH:mm:ss}\t{1}", DateTime.Now, value);
            Trace.WriteLine(string.Format("{0:yyyy/MM/dd HH:mm:ss}\t{1}", DateTime.Now, value));
            Console.WriteLine(value);
        }
    }
}
